<?php
/*-------------------------------------------------------------------------\
|
|  ========================================================
|  Fusion Registry GPL
|  Copyright (C) 2007 Fusion Scripts
|
|  This program is free software; you can redistribute it and/or
|  modify it under the terms of the GNU General Public License
|  as published by the Free Software Foundation; either version 2
|  of the License, or (at your option) any later version.
|
|  This program is distributed in the hope that it will be useful,
|  but WITHOUT ANY WARRANTY; without even the implied warranty of
|  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
|  GNU General Public License for more details.
|
|  You should have received a copy of the GNU General Public License
|  along with this program; if not, write to the Free Software
|  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
|  ========================================================
|
|  Email: info@fusionscripts.com
|  Web-Site: http://www.fusionscripts.com/
|
|  $Id: index.php 4 2008-08-03 01:43:03Z registry.fusion $
|
\-------------------------------------------------------------------------*/

/**
 * Fusion Registry 'instagrade' Upgrade System
 *
 * @package Fusion Registry
 * @subpackage upgrade
 * @version $Id: index.php 4 2008-08-03 01:43:03Z registry.fusion $
 */

// Setup
error_reporting(E_ERROR | E_WARNING | E_PARSE);
set_magic_quotes_runtime(0);

define( 'ROOT_PATH', '../../../../../' );
define( 'KERNEL_PATH', ROOT_PATH.'ips_kernel/' );
define( 'CACHE_PATH' , ROOT_PATH );
define( 'USE_SHUTDOWN', 0 );
define( 'SAFE_MODE_ON', 0 );
define( 'IN_IPB', 1 );

require './instagrade/template.php';

// Are we in the right spot?
if ( !file_exists(ROOT_PATH.'sources/ipsclass.php') )
{
    $template = new template();
    $template->install_error("Unable to locate the file: /sources/ipsclass.php<br /><br />Please ensure your forum is properly installed and that you have uploaded all Fusion Registry files to the correct location.");
    exit();
}

// Initiate super-class
$INFO = array();
require ROOT_PATH.'sources/ipsclass.php';
require ROOT_PATH.'conf_global.php';

$ipsclass = new ipsclass();
$ipsclass->vars = $INFO;
$ipsclass->template = new template();

// Upgrade locked?
if ( file_exists( './upgrade.lock') )
{
    $ipsclass->template->install_error("The upgrader is locked!<br />Please (via FTP) remove the file 'upgrade.lock' from this directory");
    exit();
}

// Start your engines!
$ipsclass->init_db_connection();
$ipsclass->parse_incoming();
$ipsclass->initiate_ipsclass();

// Get instagrade going!
require './instagrade/instagrade.php';
$instagrade = new instagrade();
$instagrade->ipsclass =& $ipsclass;

// Get our upgrade path
if ( file_exists( './version_history.php' ) )
{
    $import_versions = array();

    require_once( './version_history.php' );

	$instagrade->version_history = $import_versions;
}
else
{
	$ipsclass->template->install_error( "Could not locate the version history file: './version_history.php'");
    exit();
}

// Gather info
$instagrade->get_upgrade_kits();
$instagrade->get_current_version();
$instagrade->get_latest_upgrade_kit();

// Error check...
if ( ! $instagrade->current_version )
{
	$ipsclass->template->install_error( "Sorry, an error has occured, we are unable to determine the current version of Garage Module or Fusion Registry that is installed. If you are running Garage Module version less than v1.2.0 this upgrade system can not be used until you upgrade to at least Garage Module v1.2.0" );
    exit();
}

if ( ! $instagrade->last_upgrade_kit )
{
    $ipsclass->template->msg( "We were unable to determine if there are any required upgrade files left to run." );
    exit();
}

if ( ! $ipsclass->input['act'] )
{
	if ( $instagrade->current_version >= intval( $instagrade->current_upgrade ) )
	{
		$ipsclass->template->msg( "This Fusion Registry installation is up-to-date and there are no more upgrades required" );
	}
}

// 'Safe' acts that can be called here
$ipsclass->safe_act = array( 'finish', 'done' );


// Get and run upgrade script
if ( ! in_array( $ipsclass->input['act'], $ipsclass->safe_act ) )
{
	if ( file_exists( './upgrade_'.$instagrade->current_upgrade.'/version_upgrade.php' ) )
	{
		require_once( './upgrade_'.$instagrade->current_upgrade.'/version_upgrade.php' );

        $upgrade_kit =  new version_upgrade();
		$upgrade_kit->ipsclass =& $ipsclass;
        $upgrade_kit->instagrade =& $instagrade;

        $upgrade_kit->version_process();
	}
	else
	{
		$ipsclass->template->install_error( "Could not locate the required upgrade script: 'upgrade_{$instagrade->current_upgrade}/version_upgrade.php'");
        exit();
	}
}


// Get up and go!
switch ($ipsclass->input['act'])
{
	case 'finish':
		$instagrade->upgrade_finished();
		break;

    case 'done':
		$instagrade->upgrade_kit_done();
		break;

    case 'kit':
		$upgrade_kit->auto_run();
		break;

    default:
		$instagrade->upgrade_system_intro();
		break;
}

?>